package com.raos.xdocreport;

import com.raos.xdocreport.utils.WordUtil;
import fr.opensagres.xdocreport.core.XDocReportException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * word 导出
 *
 * @author raos
 * @emil 991207823@qq.com
 * @date 2022/2/27 18:13
 */
@RestController
@RequestMapping("/word")
public class WordController {

    @Value("${word.templatePath:''}")
    private String templatePath;

    @GetMapping("/get")
    public void getWord(HttpServletResponse response) throws IOException, XDocReportException {
        long millis = System.currentTimeMillis();
        generateWord(response);
        System.out.println(System.currentTimeMillis() - millis);
    }

    public void generateWord(HttpServletResponse response) {
        // 获取模板文件
        File file = new File(templatePath);

        Map<String, Object> context = new HashMap<>();
        context.put("toDateNum", "20220227T00001");
        context.put("dept", "成华区");
        context.put("order", "初次交办");
        context.put("nature", "主办");
        context.put("endDate", "2022-03-01 17:30:00");
        context.put("person", "饶先生");
        context.put("telephone", "15583306348");
        context.put("addr", "武侯区芳草街道");
        context.put("content", "测试来访内容");
        context.put("implDept", "成华区应急局");
        context.put("handler", "罗律师");
        context.put("verdict", "测试结论");
        context.put("approvalOpinion", "测试审批意见");
        context.put("reviewComment", "测试审核意见");
        context.put("remark", "测试备注");

        WordUtil.generateWord(response, file, context, "成都市长公开电话办理转办单20220226.docx");

    }

}
